package com.brainhealth.customer.service.impl;

import java.util.Date;
import java.util.List;

import com.brainhealth.common.enums.LearnType;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.Learn;
import com.brainhealth.customer.mapper.LearnMapper;
import com.brainhealth.customer.service.ICustomerLevelService;
import com.brainhealth.customer.service.ICustomerScoreService;
import com.brainhealth.customer.service.ICustomerService;
import com.brainhealth.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.CustomerLearnMapper;
import com.brainhealth.customer.domain.CustomerLearn;
import com.brainhealth.customer.service.ICustomerLearnService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员学习记录Service业务层处理
 * 
 * @author wolong
 * @date 2023-05-06
 */
@Service
public class CustomerLearnServiceImpl implements ICustomerLearnService 
{
    @Autowired
    private CustomerLearnMapper customerLearnMapper;
    @Autowired
    private LearnMapper learnMapper;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICustomerScoreService customerScoreService;

    /**
     * 查询会员学习记录
     * 
     * @param customerLearnId 会员学习记录主键
     * @return 会员学习记录
     */
    @Override
    public CustomerLearn selectCustomerLearnByCustomerLearnId(Long customerLearnId)
    {
        return customerLearnMapper.selectCustomerLearnByCustomerLearnId(customerLearnId);
    }

    /**
     * 查询会员学习记录列表
     * 
     * @param customerLearn 会员学习记录
     * @return 会员学习记录
     */
    @Override
    public List<CustomerLearn> selectCustomerLearnList(CustomerLearn customerLearn)
    {
        return customerLearnMapper.selectCustomerLearnList(customerLearn);
    }

    /**
     * 新增会员学习记录
     * 
     * @param customerLearn 会员学习记录
     * @return 结果
     */
    @Override
    public int insertCustomerLearn(CustomerLearn customerLearn)
    {
        customerLearn.setCreateTime(DateUtils.getNowDate());
        return customerLearnMapper.insertCustomerLearn(customerLearn);
    }

    /**
     * 修改会员学习记录
     * 
     * @param customerLearn 会员学习记录
     * @return 结果
     */
    @Override
    public int updateCustomerLearn(CustomerLearn customerLearn)
    {
        customerLearn.setUpdateTime(DateUtils.getNowDate());
        return customerLearnMapper.updateCustomerLearn(customerLearn);
    }

    /**
     * 批量删除会员学习记录
     * 
     * @param customerLearnIds 需要删除的会员学习记录主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLearnByCustomerLearnIds(Long[] customerLearnIds)
    {
        return customerLearnMapper.deleteCustomerLearnByCustomerLearnIds(customerLearnIds);
    }

    /**
     * 删除会员学习记录信息
     * 
     * @param customerLearnId 会员学习记录主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLearnByCustomerLearnId(Long customerLearnId)
    {
        return customerLearnMapper.deleteCustomerLearnByCustomerLearnId(customerLearnId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCustomerLearn(HttpServletRequest request, CustomerLearn customerLearn)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        CustomerLearn queryCustomerLearn = new CustomerLearn();
        queryCustomerLearn.setCustomerId(customer.getCustomerId());
        queryCustomerLearn.setLearnId(customerLearn.getLearnId());
        List<CustomerLearn> lstCustomerLearn = customerLearnMapper.selectCustomerLearnList(queryCustomerLearn);
        if (lstCustomerLearn.size() > 0)
        {
            return false;
        }
        //使用观看视频的时长来计算当前视频能得到的分数
        Learn queryLearn = learnMapper.selectLearnByLearnId(customerLearn.getLearnId());
        Long score = 0l;
        Long learnTime = customerLearn.getLearnTime();
        if (queryLearn.getVideoTime() != null && learnTime > queryLearn.getVideoTime())
        {
            learnTime = queryLearn.getVideoTime();
        }
        if (queryLearn.getLearnType() == LearnType.RichText)
        {
            if (customerLearn.getLearnTime() != null && customerLearn.getLearnTime() > 3)
            {
                score = 3l;
            }
        }
        if (queryLearn.getLearnType() == LearnType.LearnVideo)
        {
            Long videoLength = queryLearn.getVideoTime();
            if (learnTime != null && learnTime > 0)
            {
                double percent = (learnTime.doubleValue() / videoLength.doubleValue()) * 100;
                if (percent < 30)
                {
                    if (learnTime < 60)
                    {
                        score = 1l;
                    } else
                    {
                        score = (learnTime / 60);
                    }
                }
                if (percent > 30 && percent < 60)
                {
                    score = (videoLength / 60) / 2;
                }
                if (percent >= 60)
                {
                    score = (videoLength / 60);
                }
            } else
            {
                score = 3l;
            }
        }
        customer.setLevelValue(customer.getLevelValue() + score);
        //更新会员学习时长
        if (learnTime != null)
        {
            if (customer.getLearnTime() == null)
            {
                customer.setLearnTime(0l);
            }
            customer.setLearnTime(customer.getLearnTime() + learnTime);
        }
        customerService.updateCustomerInfo(customer);
        CustomerLearn insertCustomerLearn = new CustomerLearn();
        insertCustomerLearn.setCustomerId(customer.getCustomerId());
        insertCustomerLearn.setLearnId(customerLearn.getLearnId());
        insertCustomerLearn.setLearnTime(customerLearn.getLearnTime());
        insertCustomerLearn.setScore(score);
        insertCustomerLearn.setCreateTime(new Date());
        int result = customerLearnMapper.insertCustomerLearn(insertCustomerLearn);
        if (result > 0)
        {
            customerScoreService.setCustomerScore(request, customerLearn.getLearnId());
            return true;
        }
        return false;
    }
}
